package net.root.plain.main;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import net.root.plain.alarm.AlarmDAO;
import net.root.plain.alarm.AlarmDTO;
import net.root.plain.util.DBConn;
import net.root.plain.util.ULog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TimePicker;

public class AddActivity extends Activity {
	AlarmDTO dto = null;
	Button[] btn = new Button[2];
	ListView list;
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	Calendar calendar = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alarm_add);
		Intent intent = getIntent();
		dto = new AlarmDTO();
		int no = 0;
		AlarmDAO dao = new AlarmDAO(DBConn.getConnection(AddActivity.this));
		try {
			no = intent.getExtras().getInt(DBConn.columns[0]);
			dto = dao.getReadData(no);
		} catch (NullPointerException e) {
		}
		if (no == 0) {
			dto.setNo(dao.getMaxNo() + 1);
			dto.setAlarmBell("Bell");
			dto.setAlarmLabel("Label");
			dto.setAlarmRepeat("월");
			dto.setAlarmStatus(0);
			dto.setAlarmTime("11:00");
			dto.setAlarmVibrate(0);
			dao.insertData(dto);
		}
		DBConn.close();

		ArrayList<String> arGeneral = new ArrayList<String>();
		arGeneral.add("알람 켜기");
		arGeneral.add("시간 설정");
		arGeneral.add("반복 설정");
		arGeneral.add("벨소리 설정");
		arGeneral.add("진동 설정");
		arGeneral.add("라벨 설정");

		ArrayAdapter<String> adapter;
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, arGeneral);
		list = (ListView) findViewById(R.id.add_listView1);
		list.setAdapter(adapter);
		list.setOnItemClickListener(mItemClickListener);

		btn[0] = (Button) findViewById(R.id.add_button1);
		btn[1] = (Button) findViewById(R.id.add_button2);

		btn[0].setOnClickListener(mClickListener);
		btn[1].setOnClickListener(mClickListener);
	}

	OnClickListener mClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.add_button1:
				updateData();
				finish();
				break;
			case R.id.add_button2:
				deleteData();
				finish();
				break;
			}
		}
	};

	OnItemClickListener mItemClickListener = new OnItemClickListener() {
		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position,
				long arg3) {
			// TODO Auto-generated method stub
			ULog.showToast(AddActivity.this, dto.toString());
			showDialog(position);
		}
	};

	protected Dialog onCreateDialog(int id) {
		final LinearLayout linear = (LinearLayout) View.inflate(this,
				R.layout.list_edittext, null);
		final EditText edt = (EditText) linear.findViewById(R.id.list_editText);
		switch (id) {
		case 0:
			return new AlertDialog.Builder(AddActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("알람켜기")
					.setMultiChoiceItems(new CharSequence[] { "켜기" },
							new boolean[] { false },
							new DialogInterface.OnMultiChoiceClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which, boolean isChecked) {
									// TODO Auto-generated method stub
									dto.setAlarmStatus(isChecked ? 1 : 0);
								}
							})
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							}).create();
		case 1:
			return new TimePickerDialog(this,
					new TimePickerDialog.OnTimeSetListener() {
						@Override
						public void onTimeSet(TimePicker view, int hourOfDay,
								int minute) {
							// TODO Auto-generated method stub
							calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
							calendar.set(Calendar.MINUTE, minute);
							dto.setAlarmTime(hourOfDay + ":" + minute);
						}
					}, calendar.get(Calendar.HOUR_OF_DAY),
					calendar.get(Calendar.MINUTE), true);
		case 2:
			final CharSequence[] week = new CharSequence[] { "월", "화", "수",
					"목", "금", "토", "일" };
			boolean[] check = new boolean[] { false, false, false, false,
					false, false, false };
			return new AlertDialog.Builder(AddActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("반복 설정")
					.setMultiChoiceItems(week, check,
							new DialogInterface.OnMultiChoiceClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which, boolean isChecked) {
									// TODO Auto-generated method stub
									dto.setAlarmRepeat(week[which].toString());
								}
							})
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub

								}
							}).create();
		case 3:
			return new AlertDialog.Builder(AddActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("벨소리 설정")
					.setView(linear)
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dto.setAlarmBell(edt.getText().toString());
								}
							}).create();
		case 4:
			return new AlertDialog.Builder(AddActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("진동 설정")
					.setMultiChoiceItems(new CharSequence[] { "켜기" },
							new boolean[] { false },
							new DialogInterface.OnMultiChoiceClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which, boolean isChecked) {
									// TODO Auto-generated method stub
									dto.setAlarmVibrate(isChecked ? 1 : 0);
								}
							})
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
								}
							}).create();
		case 5:
			return new AlertDialog.Builder(AddActivity.this)
					.setIcon(R.drawable.ic_launcher)
					.setTitle("라벨 설정")
					.setView(linear)
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
									dto.setAlarmLabel(edt.getText().toString());
								}
							}).create();
		}
		return null;
	}

	public void updateData() {
		AlarmDAO dao = new AlarmDAO(DBConn.getConnection(AddActivity.this));
		dao.updateData(dto);
		DBConn.close();
	}

	public void deleteData() {
		AlarmDAO dao = new AlarmDAO(DBConn.getConnection(AddActivity.this));
		dao.deleteData(dto.getNo());
		DBConn.close();
	}
}
